package com.flyqiu.form.core.event;


import com.flyqiu.form.api.event.FlyQiuBaseFormEventManage;
import com.flyqiu.form.api.event.FlyQiuTableEvent;
import com.flyqiu.form.api.event.FlyQiuTableEventManage;
import com.flyqiu.form.model.FlyQiuColumnInfo;
import com.flyqiu.form.model.SqlColumnModel;
import com.flyqiu.form.model.entity.FlyQiuTableConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

@Slf4j
public class DefaultFlyQiuEventManage extends FlyQiuBaseFormEventManage<DefaultFlyQiuEventManage, FlyQiuTableEvent> implements FlyQiuTableEventManage {


    @Override
    public void register(FlyQiuTableEvent event) {
        addEvent(event);
    }

    @Override
    public void unregister(FlyQiuTableEvent event) {
        removeEvent(event);
    }


    /**
     * 创建数据表前
     *
     * @param formConfig
     */
    @Override
    public void beforeCreateTable(FlyQiuTableConfig formConfig) {
        List<FlyQiuTableEvent> listeners = getEvents(UNION_KEY_PREFIX, formConfig.getTableName());
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableEvent listener : listeners) {
                listener.beforeCreateTable(formConfig);
            }
        }
        listeners = null;
        if (StringUtils.isNotEmpty(formConfig.getFormCategory())) {
            listeners = getEvents(GROUP_PREFIX, formConfig.getFormCategory());
        }
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableEvent listener : listeners) {
                listener.beforeCreateTable(formConfig);
            }
        }
        listeners = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableEvent listener : listeners) {
                listener.beforeCreateTable(formConfig);
            }
        }
    }

    /**
     * 创建表后
     *
     * @param formConfig
     */
    @Override
    public void afterCreateTable(FlyQiuTableConfig formConfig) {
        List<FlyQiuTableEvent> listeners = getEvents(UNION_KEY_PREFIX, formConfig.getTableName());
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableEvent listener : listeners) {
                listener.afterCreateTable(formConfig);
            }
        }
        listeners = null;
        if (StringUtils.isNotEmpty(formConfig.getFormCategory())) {
            listeners = getEvents(GROUP_PREFIX, formConfig.getFormCategory());
        }
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableEvent listener : listeners) {
                listener.afterCreateTable(formConfig);
            }
        }
        listeners = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableEvent listener : listeners) {
                listener.afterCreateTable(formConfig);
            }
        }
    }

    /**
     * 更新数据表前
     *
     * @param formConfig 表新配置
     * @param oldColumn  表旧列
     */
    @Override
    public void beforeUpdateTable(FlyQiuTableConfig formConfig, List<SqlColumnModel> oldColumn) {

        List<FlyQiuTableEvent> listeners = getEvents(UNION_KEY_PREFIX, formConfig.getTableName());
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableEvent listener : listeners) {
                listener.beforeUpdateTable(formConfig, oldColumn);
            }
        }
        listeners = null;
        if (StringUtils.isNotEmpty(formConfig.getFormCategory())) {
            listeners = getEvents(GROUP_PREFIX, formConfig.getFormCategory());
        }
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableEvent listener : listeners) {
                listener.beforeUpdateTable(formConfig, oldColumn);
            }
        }
        listeners = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableEvent listener : listeners) {
                listener.beforeUpdateTable(formConfig, oldColumn);
            }
        }
    }

    /**
     * 更新数据表后
     *
     * @param formConfig
     */
    @Override
    public void afterUpdateTable(FlyQiuTableConfig formConfig) {

        List<FlyQiuTableEvent> listeners = getEvents(UNION_KEY_PREFIX, formConfig.getTableName());
        ;
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableEvent listener : listeners) {
                listener.afterUpdateTable(formConfig);
            }
        }
        listeners = null;
        if (StringUtils.isNotEmpty(formConfig.getFormCategory())) {
            listeners = getEvents(GROUP_PREFIX, formConfig.getFormCategory());
        }
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableEvent listener : listeners) {
                listener.afterUpdateTable(formConfig);
            }
        }
        listeners = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableEvent listener : listeners) {
                listener.afterUpdateTable(formConfig);
            }
        }
    }

    /**
     * 同步数据表前
     *
     * @param formConfig 数据表配置
     */
    @Override
    public void beforeSynchronizationTable(FlyQiuTableConfig formConfig) {

        List<FlyQiuTableEvent> listeners = getEvents(UNION_KEY_PREFIX, formConfig.getTableName());
        ;
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableEvent listener : listeners) {
                listener.beforeSynchronizationTable(formConfig);
            }
        }
        listeners = null;
        if (StringUtils.isNotEmpty(formConfig.getFormCategory())) {
            listeners = getEvents(GROUP_PREFIX, formConfig.getFormCategory());
        }
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableEvent listener : listeners) {
                listener.beforeSynchronizationTable(formConfig);
            }
        }
        listeners = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableEvent listener : listeners) {
                listener.beforeSynchronizationTable(formConfig);
            }
        }
    }


    @Override
    public void afterSynchronizationTable(FlyQiuTableConfig formConfig, List<SqlColumnModel> newColumn, FlyQiuColumnInfo flyQiuColumnInfo) {
        List<FlyQiuTableEvent> listeners = getEvents(UNION_KEY_PREFIX, formConfig.getTableName());
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableEvent listener : listeners) {
                listener.afterSynchronizationTable(formConfig, newColumn, flyQiuColumnInfo);
            }
        }
        listeners = null;
        if (StringUtils.isNotEmpty(formConfig.getFormCategory())) {
            listeners = getEvents(GROUP_PREFIX, formConfig.getFormCategory());
        }
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableEvent listener : listeners) {
                listener.afterSynchronizationTable(formConfig, newColumn, flyQiuColumnInfo);
            }
        }
        listeners = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableEvent listener : listeners) {
                listener.afterSynchronizationTable(formConfig, newColumn, flyQiuColumnInfo);
            }
        }
    }

    /**
     * 删除数据表之前
     *
     * @param formConfig
     */
    @Override
    public void beforeDeleteTable(FlyQiuTableConfig formConfig) {

        List<FlyQiuTableEvent> listeners = getEvents(UNION_KEY_PREFIX, formConfig.getTableName());
        ;
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableEvent listener : listeners) {
                listener.beforeDeleteTable(formConfig);
            }
        }
        listeners = null;
        if (StringUtils.isNotEmpty(formConfig.getFormCategory())) {
            listeners = getEvents(GROUP_PREFIX, formConfig.getFormCategory());
        }
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableEvent listener : listeners) {
                listener.beforeDeleteTable(formConfig);
            }
        }
        listeners = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableEvent listener : listeners) {
                listener.beforeDeleteTable(formConfig);
            }
        }
    }

    /**
     * 删除数据表之后
     *
     * @param formConfig
     */
    @Override
    public void afterDeleteTable(FlyQiuTableConfig formConfig) {

        List<FlyQiuTableEvent> listeners = getEvents(UNION_KEY_PREFIX, formConfig.getTableName());
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableEvent listener : listeners) {
                listener.afterDeleteTable(formConfig);
            }
        }
        listeners = null;
        if (StringUtils.isNotEmpty(formConfig.getFormCategory())) {
            listeners = getEvents(GROUP_PREFIX, formConfig.getFormCategory());
        }
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableEvent listener : listeners) {
                listener.afterDeleteTable(formConfig);
            }
        }
        listeners = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableEvent listener : listeners) {
                listener.afterDeleteTable(formConfig);
            }
        }
    }

    /**
     * 清空数据表前
     *
     * @param formConfig
     */
    @Override
    public void beforeClearData(FlyQiuTableConfig formConfig) {

        List<FlyQiuTableEvent> listeners = getEvents(UNION_KEY_PREFIX, formConfig.getTableName());
        ;
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableEvent listener : listeners) {
                listener.beforeClearData(formConfig);
            }
        }
        listeners = null;
        if (StringUtils.isNotEmpty(formConfig.getFormCategory())) {
            listeners = getEvents(GROUP_PREFIX, formConfig.getFormCategory());
        }
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableEvent listener : listeners) {
                listener.beforeClearData(formConfig);
            }
        }
        listeners = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableEvent listener : listeners) {
                listener.beforeClearData(formConfig);
            }
        }
    }

    /**
     * 清口数据表后
     *
     * @param formConfig
     */
    @Override
    public void afterClearData(FlyQiuTableConfig formConfig) {

        List<FlyQiuTableEvent> listeners = getEvents(UNION_KEY_PREFIX, formConfig.getTableName());
        ;
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableEvent listener : listeners) {
                listener.afterClearData(formConfig);
            }
        }
        listeners = null;
        if (StringUtils.isNotEmpty(formConfig.getFormCategory())) {
            listeners = getEvents(GROUP_PREFIX, formConfig.getFormCategory());
        }
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableEvent listener : listeners) {
                listener.afterClearData(formConfig);
            }
        }
        listeners = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableEvent listener : listeners) {
                listener.afterClearData(formConfig);
            }
        }
    }


}
